#include "print.h"
#include "common_fun.h"

PRINT::PRINT()
{

}

int PRINT::command_print(QString label,MACHINE_SETTING* machine_setting)
{
    DLL* function = new DLL();

    qDebug()<<"label is :"+label;
    //如果输入框内容为空，就返回失败标记。
    if(label == "")
    {
        return -1;
    }
    //获得驱动名称
    QString machine_driver = machine_setting->get_NORMAL().Machine_driver;
    //获得打标模式
    QString code_type = machine_setting->get_NORMAL().Code_type;


    //根据打标模式构造内建指令
    QStringList cmd_List;
    if(code_type == "txt")
    {
        QStringList line = label.split("\n");

        int index;
        int X_Location = machine_setting->get_NORMAL().label_offset_width;
        int Y_Location = machine_setting->get_NORMAL().label_offset_height;
        int line_distance = machine_setting->get_NORMAL().line_distance;
        int label_height = machine_setting->get_TEXT().text_horizontal*15;

        for(index = 0;index < line.count();index++)
        {
            //如果读到空行，就什么都不做。由于index还是增加了，所以标签上会体现出一个空行。
            if(line.at(index) == "")
            {continue;}

            //Y_location + index*(line_distance+label_height) 通过这个式子来计算出每一行的位置
            QString temp = cmd_build_TEXT(X_Location,Y_Location+index*(line_distance+label_height),line.at(index),machine_setting);
            //将指令添加到指令队列
            cmd_List.append(temp);
        }
    }
    else if(code_type == "BARcode")
    {

        QStringList line = label.split("\n");

        int index;
        int X_Location = machine_setting->get_NORMAL().label_offset_width;
        int Y_Location = machine_setting->get_NORMAL().label_offset_height;
        int line_distance = machine_setting->get_NORMAL().line_distance;
        int label_height = machine_setting->get_BARCODE().barcode_height;

        for(index = 0;index < line.count();index++)
        {
            //如果读到空行，就什么都不做。由于index还是增加了，所以标签上会体现出一个空行。
            if(line.at(index) == "")
            {continue;}

            //Y_location + index*(line_distance+label_height) 通过这个式子来计算出每一行的位置
            QString temp = cmd_build_BARcode(X_Location,Y_Location+index*(line_distance+label_height),line.at(index),machine_setting);
            //将指令添加到指令队列
            cmd_List.append(temp);
        }
    }
    else if(code_type == "QRcode")
    {
        //QRcode可以承载很多信息，所以这里就不进行多行打印的操作了。
        int X_Location = machine_setting->get_NORMAL().label_offset_width;
        int Y_Location = machine_setting->get_NORMAL().label_offset_height;

         QString temp = cmd_build_QRcode(X_Location,Y_Location,label,machine_setting);
         //将指令添加到指令队列
         cmd_List.append(temp);
    }
    else
    {
        //返回失败标记
        return -1;
    }
    // 需要把符号 " 替换成字符串 \["] 才能打印出符号 " 。

    //把utf-8编码转换为机器可以识别的GBK编码
    QTextCodec* utf8 = QTextCodec::codecForName("UTF-8");
    QTextCodec::setCodecForLocale(utf8);
    QTextCodec* gbk = QTextCodec::codecForName("gbk");
    //QTextCodec* gb18030 = QTextCodec::codecForName("GB18030");

    //打开打印机并清空缓存
    QString utf8_machine_driver = utf8->toUnicode(machine_driver.toLocal8Bit().data());
    QByteArray gbk_machine_driver = gbk->fromUnicode(utf8_machine_driver);
    function->OPENPORT(gbk_machine_driver.data());
    function->CLEARBUFFER();

    //把命令队列内的所有指令依次输入打印机
    for(int i = 0;i<cmd_List.count();i++)
    {
        qDebug()<<"cmd is :"+cmd_List.at(i);
        QString utf8_cmd = utf8->toUnicode(cmd_List.at(i).toLocal8Bit().data());
        QByteArray gbk_cmd = gbk->fromUnicode(utf8_cmd);

        function->SENDCOMMAND(gbk_cmd.data());

        //这个函数不在DLL文档中，但是出现在了技术文档中。实践证明，这个函数是DLL内没有的
        //function->SENDBINARYDATA(gbk_cmd.data(),gbk_cmd.length());
    }


    //准备好指令之后，调用打印机
    char copies[2] = "1"; //打印份数，以字符串形式保存
    char counts[2] = "1";  //每份多少张，以字符串形式保存
    function->PRINTLABEL(copies,counts);  //只打印1张
    function->CLOSEPORT();

    return 1;
}

QString PRINT::cmd_build_BARcode(int X,int Y,QString label,MACHINE_SETTING* machine_setting)    //构建一维条码打印指令
{
    QString cmd = "";
    //BARCODE X, Y, ”code type”, height, human readable, rotation, narrow, wide, “code”

    cmd.append("BARCODE ");
    //cmd.append(int_2_qstring(machine_setting->get_NORMAL().label_offset_width));
    cmd.append(int_2_qstring(X));
    cmd.append(",");
    //cmd.append(int_2_qstring(machine_setting->get_NORMAL().label_offset_height));
    cmd.append(int_2_qstring(Y));
    cmd.append(",");
    cmd.append('\"'+machine_setting->get_BARCODE().barcode_type+'\"');
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_BARCODE().barcode_height));
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_BARCODE().barcode_human));
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_BARCODE().barcode_turn));
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_BARCODE().barcode_A));
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_BARCODE().barcode_B));
    cmd.append(",");
    label.replace("\"","\\[\"]"); //如果有英文单引号字符 " ，需要在输入中这样替换才能打印出单引号字符。 \" = \\[\"]
    cmd.append('\"'+label+'\"');
    return cmd;
}
QString PRINT::cmd_build_TEXT(int X,int Y,QString label,MACHINE_SETTING* machine_setting)
{
    QString cmd = "";
    //TEXT X, Y, ”font”, rotation, x-multiplication, y-multiplication, ”content”

    cmd.append("TEXT ");
    //cmd.append(int_2_qstring(machine_setting->get_NORMAL().label_offset_width));
    cmd.append(int_2_qstring(X));
    cmd.append(",");
    //cmd.append(int_2_qstring(machine_setting->get_NORMAL().label_offset_height));
    cmd.append(int_2_qstring(Y));
    cmd.append(",");
    cmd.append('\"'+machine_setting->get_TEXT().text_type+'\"');
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_TEXT().text_turn));
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_TEXT().text_horizontal));
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_TEXT().text_vertical));
    cmd.append(",");
    label.replace("\"","\\[\"]"); //如果有英文单引号字符 " ，需要在输入中这样替换才能打印出单引号字符。 \" = \\[\"]
    cmd.append('\"'+label+'\"');
    return cmd;
}
QString PRINT::cmd_build_QRcode(int X,int Y,QString label,MACHINE_SETTING* machine_setting)
{
    QString cmd = "";
    //QRCODE X, Y, ECC Level, cell width, mode, rotation, [model, mask,]"Data string”

    cmd.append("QRCODE ");
    //cmd.append("PDF417 "); 佳博的标签机不支持PDF417格式
    cmd.append(int_2_qstring(X));
    cmd.append(",");
    cmd.append(int_2_qstring(Y));
    cmd.append(",");
    cmd.append(machine_setting->get_QRCODE().qrcode_ecc);
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_QRCODE().qrcode_cell));
    cmd.append(",");
    cmd.append(machine_setting->get_QRCODE().qrcode_mode);
    cmd.append(",");
    cmd.append(int_2_qstring(machine_setting->get_QRCODE().qrcode_turn));
    cmd.append(",");
    //cmd.append("[");// 字符[ 和 字符] 无需加入到命令里面去
    cmd.append(int_2_qstring(machine_setting->get_QRCODE().qrcode_model));
    cmd.append(",");
    cmd.append(machine_setting->get_QRCODE().qrcode_mode);
    cmd.append(",");
    //cmd.append("]");
    label.replace("\"","\\[\"]"); //如果有英文单引号字符 " ，需要在输入中这样替换才能打印出单引号字符。 \" = \\[\"]

    //label = "K" + label;    //虽然编程手册上写道，用QRcode包含中文或者日文时需要加入前缀K
                              //但是实践证明，这并没有所用。

    //如果字符串末尾全都是回车或者空格，就将这部分回车删除。
    while(label.at(label.length()-1) == ' ' || label.at(label.length()-1) == '\n')
    {
        label.remove(label.length()-1,1);
    }

    //如果字符串中间出现回车，需要把回车替换为空格。
    label.replace("\n"," ");

    cmd.append('\"'+label+'\"');
    return cmd;
}
